/*
 * Copyright (C) 2007, 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.takaharabooks.mhp3rd;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;  
import android.graphics.Bitmap.Config;  
import android.graphics.Canvas; 
import android.graphics.Rect;
import android.graphics.Color;
import android.text.style.RelativeSizeSpan;
import android.text.util.Linkify;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.graphics.Paint;
import android.widget.Toast;
// view
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Spinner;
import jp.co.nobot.libAdMaker.libAdMaker;
// Dialog
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;


public class ArmorList extends Activity implements Runnable 
{
	//**********************定数定義*************************
	// 背景用一マスのサイズ
	public final static int MISSION_NONE				= 0;
	public final static int MISSION_CORNER_ONE			= 1;
	public final static int MISSION_CORNER_TWO			= 2;
	public final static int MISSION_CORNER_THREE		= 3;
	public final static int MISSION_CORNER_FOUR			= 4;
	
	//public final static int TEXT_VIEW_ABC_TBL[] = {
	//	R.id.TextView00,
	//	R.id.TextView01,
	//	R.id.TextView02,
	//	R.id.TextView03,
	//	R.id.TextView04,
	//	R.id.TextView05,
	//	R.id.TextView06,
	//	R.id.TextView07
	//};
	
	//******************************************************

	// 広告クラス
	private ConbinateAdView m_csAd;

	  private Context m_csContext;
	  //private TextView[]  m_csABCText;
	  //static private int m_nABCViewID = R.id.TextView00;
	  Bitmap m_selectedBitmap;
	  // 絞り込み用
	  private int m_nSelectedKind = 1;
	  private int m_nSelectedRare = 1;
	  private String m_strSelectedSkill = "";
	  private int m_nSelectedSkill = -1;
	  private int m_nSelectedPart = -1;
	  private int m_nRareNum = 0;
	  
	  // プログレスダイアログ
	  static private ProgressDialog waitDialog = null;
	    
	    // DB
	  DB_ArmorList m_dbArmor = null;
	  
	  // 作成View情報
	  int m_nNowItemPos;
	  TextView m_csArmorListViews[];
      
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.armor_list);
        
        // 広告表示
        m_csAd = new ConbinateAdView( (ProgressBar)findViewById(R.id.loading2) );
        m_csAd.InitAdMaker((libAdMaker)findViewById(R.id.admakerview2), this);
        m_csAd.showAdMaker();
        
    	// もしまだ表示されていたら消去
    	CloseProgressDlg();
    	
        // 読み込み中のプログレスダイアログを表示
        waitDialog = new ProgressDialog(this);
        waitDialog.setTitle("Loading...");
        waitDialog.setMessage("データ読み込み中です。\nしばらくお待ちください...");
        waitDialog.setIndeterminate(false);
        waitDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        waitDialog.show();
        
		// 処理開始
        Thread thread = new Thread(this);
        thread.start();
        
   	}
    
    @Override
    public void onStart()
    {

        super.onStart();
    }
    
    @Override
    public void onPause()
    {
    	CloseProgressDlg();
    	super.onPause();
    }
    
    @Override
    public void onDestroy(){
    	if( null != m_dbArmor ){
    		m_dbArmor.CloseDB();    	
    	}
    	super.onDestroy();
    }
    
    @Override
	public void run()
    {
        // 時間のかかる処理をここに記述。    	
        // データベースを作成
    	if( null == m_dbArmor ){
    		m_dbArmor = new DB_ArmorList(this); 
    		m_dbArmor.InitDB();	                    
    	}

        handler.sendEmptyMessage(0);
    }
    
    private Handler handler = new Handler()
    {
        @Override
		public void handleMessage(Message msg) {
            // 処理終了時の動作をここに記述。
        	
        	// 絞込み初期化
      		InitKindSpinner();
      		InitRareSpinner();
      		InitSkillSpinner();
     		
        	// 初期化
        	InitArmorNameList();        	
        	InitPartText();
        	InitAllView();
    		
        	CloseProgressDlg();
    		
    		// アップデート時のメッセージを表示します
    		if(m_dbArmor.m_bUpdateFlag){
    			ShowUpdateMessage();
    		}
        }
    };
    
    // プログレスダイアログの削除
    private void CloseProgressDlg()
    {
		if(waitDialog != null && waitDialog.isShowing()){
			waitDialog.dismiss();
			waitDialog = null;
		}    	
    }

    // DBから情報を取得し、ＤＢクラスにセット
    private boolean SetDB()
    {
    	return m_dbArmor.SetDB(m_nSelectedKind, m_nSelectedRare, m_strSelectedSkill, m_nSelectedPart);
    }
    
    // すべてのViewを初期化
    private void InitAllView()
    {
  		// 全種別を表示できるように初期化
  		m_nSelectedKind = 1;
  		m_nSelectedRare = 1;
  		m_strSelectedSkill = "";
    	
    	// 防具名情報のセット
    	SetArmorInfo();

    }
    
    /*************************************
     * アップデート時の１回だけメッセージを表示
     **************************************/
    private void ShowUpdateMessage()
    {
    	String strMessage = String.format(
    			"【Ver.5.3.4】" + 
    			"\n" +
    			"\n" +
    			"①剣士防具のレベル別防御力グラフの調査中解除\n" + 
    			"②単品装備追加\n" + 
    			"\n" +
    			"\n"
    	);
    	
    	//TextView csDlgTextView = new TextView(this);
    	//csDlgTextView.setAutoLinkMask(Linkify.WEB_URLS);
    	//csDlgTextView.setText(strHtml);
    	
    	new AlertDialog.Builder(this)
    	.setTitle("アップデート情報")
    	.setMessage(strMessage)
    	.setPositiveButton("OK", null)  
    	.show();  
    }
    
    /**
     * メニューを作成する
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        super.onCreateOptionsMenu(menu);

        // タイトルメニューの作成
        MenuItem csMenu1 = menu.add(Menu.NONE, Menu.FIRST, Menu.NONE, "タイトルへ");
        csMenu1.setIcon(android.R.drawable.ic_menu_revert);
        MenuItem csMenu2 = menu.add(Menu.NONE, Menu.FIRST+1, Menu.NONE, "フルスクリーン");
        csMenu2.setIcon(android.R.drawable.ic_menu_view);
        MenuItem csMenu3 = menu.add(Menu.NONE, Menu.FIRST+2, Menu.NONE, "アップデート情報");
        csMenu3.setIcon(android.R.drawable.ic_menu_upload);
        
        return true;
    }
   /**
     * メニューボタン押下時の処理
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem menu)
    {
        switch (menu.getItemId()) {
        case Menu.FIRST:
            //Activity終了
            finish();
            return true;
        case Menu.FIRST+1:
            //フルスクリーン処理
        	if(0 == (getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN)){
	        	getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
	        	menu.setTitle("通常の画面");
        	}else{
                //フルスクリーンなら元に戻す
	        	getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
	        	menu.setTitle("フルスクリーン");
        	}
            return true;
        case Menu.FIRST+2:
            //アップデート情報
        	ShowUpdateMessage();
            return true;
        default:
            break;
        }
        
        return super.onOptionsItemSelected(menu);
    }
        
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        //if (requestCode == SHOW_EDITOR) {
            if (resultCode > 0) {
            	Bundle extras = data.getExtras(); 
            	if (extras != null){
	            	int nLand = extras.getInt("LAND");
	            	if(nLand == getResources().getConfiguration().orientation){
	            		//m_dbArmor.SetDB(m_nSelectedKind, m_nSelectedStar);
		            	// ギャラリー初期
		            	//RemakeGallery(requestCode);
	            	}
            	}
          	}
        //}
    }
    
    /*************************************
     * 防具部位テキストを初期化
     **************************************/
    private void InitPartText(){
 	   TextView strPartTextView = (TextView) findViewById(R.id.ArmorPartTextView);
        strPartTextView.setOnClickListener(new View.OnClickListener(){
      	   TextView strPartTextView = (TextView) findViewById(R.id.ArmorPartTextView);
 	       	@Override
 			public void onClick(View v){
 	       		if(!IsOnlyDispPart()){	// 絞り込んでいないなら、絞り込む
 		       		m_nSelectedPart = m_dbArmor.GetArmorPartNum(m_nNowItemPos);
 	 	       		strPartTextView.setBackgroundResource(R.drawable.z_frame04);
 	       		}else{					// 絞り込んでいるなら、絞込解除
 	       			m_nSelectedPart = -1;
 	 	       		strPartTextView.setBackgroundResource(R.drawable.frame01);
 	       		}
 	       		DispMessagePart((String)strPartTextView.getText());
 	       		SetDB();
 				// 各情報の再設定
 	       		InitArmorNameList();	// リストも初期化
 	       		SetArmorInfo();
 	       	}
        });
    }
    
    /*************************************
     * 絞込みのメッセージ表示
     **************************************/
    public void DispMessagePart(String strMsg){
		if(IsOnlyDispPart()){	// 絞り込む
       		Toast.makeText(this, "部位「"+strMsg+"」で絞り込み", Toast.LENGTH_SHORT).show();
       	}else{					// 絞込解除
       		Toast.makeText(this, "部位絞り込み解除", Toast.LENGTH_SHORT).show();
       	}    	
    }
        
    /*************************************
     * 絞込みのメッセージ表示
     **************************************/
    public void DispMessageSkill(String strMsg){
    	Toast.makeText(this, "スキル「"+strMsg+"」がありません", Toast.LENGTH_SHORT).show();
    }

    /*************************************
     * 種別絞込み機能の初期化
     **************************************/
    public void InitKindSpinner()
    {
    	// スピナーボタンに表示するテキスト
    	ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
    	// スピナーを押したときに表示されるドロップダウンアイテム
    	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	
    	// ｱｲﾃﾑ種別を追加
    	int nTableNum = DB_ArmorList.ARMOR_KINDS.length;
    	for(int nIndex=0; nIndex<nTableNum; nIndex++){
    		adapter.add(DB_ArmorList.ARMOR_KINDS[nIndex]);
    	}    	
    	
    	Spinner csKindSpinner = (Spinner) findViewById(R.id.ArmorKindSpinner);
  		csKindSpinner.setAdapter(adapter);
  		csKindSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
  		{  			
  			@Override
    		public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
    			// データベースを作り変える
  				m_nSelectedKind = (int)id+1;
  				m_nSelectedRare = 1;
  				m_strSelectedSkill = "";
  				if(DB_ArmorList.ARMOR_KINDS.length == m_nSelectedKind) m_nSelectedKind = -1;
  				InitRareSpinner();
  				SetDB();
  				InitArmorNameList();	// リストも初期化
 	       		SetArmorInfo();
    		}
  			
  			@Override
  			public void onNothingSelected(AdapterView<?> arg0) {
            }
    	});
    }
    
    /*************************************
     * ﾚｱ度絞込み機能の初期化
     **************************************/
    public void InitRareSpinner()
    {
    	// スピナーボタンに表示するテキスト
    	ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
    	// スピナーを押したときに表示されるドロップダウンアイテム
    	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	
    	// ｱｲﾃﾑ種別を追加
    	int nTableNum = 0;
    	if( DB_ArmorList.ARMOR_KIND_COMMON == m_nSelectedKind ){
    		// 共用ﾚｱ度
	    	nTableNum = DB_ArmorList.ARMOR_RARES_COMMON.length;
	    	for(int nIndex=0; nIndex<nTableNum; nIndex++){
	    		adapter.add(DB_ArmorList.ARMOR_RARES_COMMON[nIndex]);
	    	}
    	}else{
    		// 剣士・ガンナーﾚｱ度
	    	nTableNum = DB_ArmorList.ARMOR_RARES.length;
	    	for(int nIndex=0; nIndex<nTableNum; nIndex++){
	    		adapter.add(DB_ArmorList.ARMOR_RARES[nIndex]);
	    	}
    	}
    	m_nRareNum = nTableNum;
    	
    	Spinner csRareSpinner = (Spinner) findViewById(R.id.ArmorRareSpinner);
    	csRareSpinner.setAdapter(adapter);
    	csRareSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
  		{  			
  			@Override
    		public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
    			// データベースを作り変える
  				if(m_nRareNum != (int)id+1)	m_nSelectedRare = GetRareNumFromSpinner((int)id);
  				else 						m_nSelectedRare = -1;
  				// 見つからない場合はリセット
  				if( !SetDB() ){
  	  				// 部位絞り込みリセット
  	  				if(IsOnlyDispPart()){
  	  					ResetSearchPart();
  	  				}
  					ResetSearchSkill();
  				}
  				InitArmorNameList();	// リストも初期化
 	       		SetArmorInfo();
    		}
  			
  			@Override
  			public void onNothingSelected(AdapterView<?> arg0) {
            }
    	});
    }
    
    /*************************************
     * ﾚｱ度絞込み機能の初期化
     **************************************/
    public int GetRareNumFromSpinner(int nIndex)
    {
    	int nRareNum = 1;
    	try{
	    	if( DB_ArmorList.ARMOR_KIND_COMMON == m_nSelectedKind ){
	    		nRareNum = Integer.parseInt(DB_ArmorList.ARMOR_RARES_COMMON[nIndex].substring(2));
	    	}else{
	    		nRareNum = Integer.parseInt(DB_ArmorList.ARMOR_RARES[nIndex].substring(2));
	    	}
    	}catch(Exception e){
    		;
    	}
    	return nRareNum;
    }
    
    /*************************************
     * ﾚｱ度絞込み機能の初期化
     **************************************/
    public void InitSkillSpinner()
    {
    	// スピナーボタンに表示するテキスト
    	ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
    	// スピナーを押したときに表示されるドロップダウンアイテム
    	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	
    	// ｱｲﾃﾑ種別を追加
    	int nTableNum = 0;
		// 共用ﾚｱ度
    	nTableNum = m_dbArmor.m_stStrSkill.length;
    	adapter.add("全スキル");
    	for(int nIndex=0; nIndex<nTableNum; nIndex++){
    		adapter.add(m_dbArmor.m_stStrSkill[nIndex]);
    	}
    	
    	Spinner csSkillSpinner = (Spinner) findViewById(R.id.ArmorSkillSpinner);
    	csSkillSpinner.setAdapter(adapter);
    	csSkillSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
  		{  			
  			@Override
    		public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
    			// データベースを作り変える
  				int tempNum = m_nSelectedSkill;
  				String tempStr = m_strSelectedSkill;
  				if(id==0){
  					m_nSelectedSkill = -1;
  					m_strSelectedSkill = "";
  				}else{
  					m_nSelectedSkill = (int)id-1;
  					m_strSelectedSkill = m_dbArmor.m_stStrSkill[m_nSelectedSkill];
  				}
  				// スキルが見つからない場合はリセット
  				if( !SetDB() ){
  					ResetSearchSkill();
  				}
  				InitArmorNameList();	// リストも初期化
 	       		SetArmorInfo();
    		}
  			
  			@Override
  			public void onNothingSelected(AdapterView<?> arg0) {
            }
    	});
    }
    
    /*************************************
     * ﾚｱ度の絞込みをリセットします
     **************************************/
    public void ResetSearchPart()
    {
		m_nSelectedPart = -1;
		TextView strPartTextView = (TextView) findViewById(R.id.ArmorPartTextView);
   		strPartTextView.setBackgroundResource(R.drawable.frame01);
    	DispMessagePart((String)strPartTextView.getText());
    	SetDB();
    }
    
    /*************************************
     * スキルの絞込みをリセットします
     **************************************/
    public void ResetSearchSkill()
    {
    	if(IsOnlyDispSkill()){
    		DispMessageSkill(m_strSelectedSkill);
    	}
    	m_nSelectedSkill = -1;
		m_strSelectedSkill = "";
    	Spinner csSkillSpinner = (Spinner) findViewById(R.id.ArmorSkillSpinner);
    	csSkillSpinner.setSelection(0);    	
    }
    
    /*************************************
     * 金額をセット
     **************************************/
    public String MakeMoneyString(String strName, int nMoney)
    {
        String strMoney = String.format("%s：%6dz", strName, nMoney);
        return strMoney;
    }

    /*************************************
     * 画面サイズ（幅）を取得
     **************************************/
    public int GetWindowWidthSize()
    {
        WindowManager windowmanager = (WindowManager)getSystemService(WINDOW_SERVICE);
        Display disp = windowmanager.getDefaultDisplay();
        return disp.getWidth();
    }

    // 長押し時の処理
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        //menu.add(R.string.str_weakpoint);
    }
    
    // アイテム選択時の処理
    @Override
    public boolean onContextItemSelected(MenuItem item) {
//        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
//        Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT).show();
        return true;
    }

    /*************************************
     * ビットマップを指定の大きさにリサイズします
     * [I-O]csBmp:リサイズする画像
     * [IN.]nW：横幅
     * [IN.]nH:縦幅（０の場合、横幅の倍率に合わせてリサイズ）
     * return: リサイズした画像を返す
     **************************************/
    public Bitmap ResizeBitmap(Bitmap csBmp, int nW, int nH){
        int width = csBmp.getWidth();
        int height = csBmp.getHeight();
        int newWidth = nW;
        int newHeight = nH;

        // calculate the scale - in this case = 0.4f
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        if(newHeight == 0){
        	scaleHeight = scaleWidth;
        }

        // createa matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);
        // rotate the Bitmap
        //matrix.postRotate(45);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(csBmp, 0, 0,
                          width, height, matrix, true);
        csBmp = resizedBitmap;

        return resizedBitmap;
    }
    
    
    /*************************************
     * 防御力情報をセット
     **************************************/
    public void SetArmorInfo()
    {
		// 防具名を表示
	    SetArmorNameText();		
    	// 防具情報グラフセット
    	SetArmorGraph();	   
    	// 属性グラフセット
    	SetResistGraph();
    	// 防具部位のセット
    	SetPartText();
    	
    	// スキル＆素材情報セット
    	SetSkillList();
    	SetMaterialList();    	
    }
    
    /*************************************
     * 防具情報グラフのセット
     **************************************/
    public void SetArmorGraph()
    {
    	ImageView imageGraph = (ImageView) findViewById(R.id.ArmorGraphImageView);
    	imageGraph.setImageBitmap(MakeArmorGraph(imageGraph.getWidth()));
    }
    
    /*************************************
     * 属性グラフのセット
     **************************************/
    public void SetResistGraph()
    {
    	ImageView imgResist = (ImageView) findViewById(R.id.ArmorResistInfoImageView);
    	imgResist.setImageBitmap(MakeResistGraph());
    }
    
    /*************************************
     * スキル一覧のセット
     **************************************/
    public void SetSkillList()
    {
    	TextView strSkill = (TextView) findViewById(R.id.ArmorSkillTextView);
    	strSkill.setText(Html.fromHtml(GetSkillListDescription()));
    }
    
    /*************************************
     * 素材一覧のセット
     **************************************/
    public void SetMaterialList()
    {
    	TextView strMaterial = (TextView) findViewById(R.id.ArmorMaterialTextView);
    	strMaterial.setText(Html.fromHtml(GetMaterialListDescription()));
    }
    
        
    /*************************************
     * 防具名リストの作成
     **************************************/
    public void InitArmorNameList()
    {
    	Resources res = getResources();
    	LinearLayout csLayout = (LinearLayout) findViewById(R.id.ArmorListLinerLayout);
    	if(0 < csLayout.getChildCount()){
    		csLayout.removeAllViews();
    	}
    	
    	int nItemListNum = m_dbArmor.GetArmorDataNum();
    	m_csArmorListViews = new TextView[nItemListNum];
    	
    	// 件数表示
    	SetListNumTextView();

    	for(int nNum=0; nNum<nItemListNum; nNum++)
    	{
    		String strName = m_dbArmor.GetArmorNameStr(nNum);
            // 色変更    		
    		//CharSequence source = Html.fromHtml("<font color=blue>" + strName + "</font>");
    		CharSequence source = Html.fromHtml(strName);
            
            // リサイズ
            SpannableString spannable = new SpannableString(source);
            RelativeSizeSpan spanResize = new RelativeSizeSpan(1.8f); 
            spannable.setSpan(spanResize, 0, strName.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);		
    		
    		TextView csView = new TextView(this);
    		csView.setSingleLine(true);
	    	csView.setText(spannable);
	    	csView.setLayoutParams(csLayout.getLayoutParams());
	    	csView.setBackgroundDrawable(res.getDrawable(R.drawable.z_frame02));
	    	csLayout.addView(csView,
	    			new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
	    										  ViewGroup.LayoutParams.WRAP_CONTENT, 1));
	    	// テキストカラーをセット
	    	SetRareColorTextView(csView, m_dbArmor.GetArmorRare(nNum));
	    	
	    	// 現在表示中の値を変更
	    	m_nNowItemPos = nNum;
	    	m_csArmorListViews[nNum] = csView;	    	
	    	
	    	// クリックされたときの動作を定義
	    	csView.setOnClickListener(new View.OnClickListener(){
	        	Resources res = getResources();
	    		private int m_nNum = m_nNowItemPos;
	    		@Override
	    		public void onClick(View v){
	    			// カーソル処理
	    			m_csArmorListViews[m_nNowItemPos].setBackgroundDrawable(res.getDrawable(R.drawable.z_frame02));
	    			m_nNowItemPos = m_nNum;
	    			m_csArmorListViews[m_nNum].setBackgroundDrawable(res.getDrawable(R.drawable.z_frame03));
	    			// ＤＢセット
	    	    	m_dbArmor.SetLevelDB(m_dbArmor.GetArmorLvID(m_nNowItemPos));
	    			// 各情報の再設定
	    			SetArmorInfo();
	    			SetPartText();
	    		}
	    	});
    	}

    	// 初期化
    	if(0 < nItemListNum){
	    	m_nNowItemPos = 0;  
	    	m_csArmorListViews[0].setBackgroundDrawable(res.getDrawable(R.drawable.z_frame03));
	    	m_dbArmor.SetLevelDB(m_dbArmor.GetArmorLvID(0));
	    	ScrollView sc = (ScrollView) findViewById(R.id.ArmorListScrollView);
	    	sc.scrollTo(0, 0);
    	}
    	//InitItemDescription(0);
    }
    
    /*************************************
     * 件数の表示セット
     **************************************/
   public void SetListNumTextView()
   {
    	TextView strListNum = (TextView) findViewById(R.id.ListNumTextView);
        CharSequence szNum = Html.fromHtml(String.format("全%d件", m_dbArmor.GetArmorDataNum()));
    	strListNum.setText(szNum);
   }

   /*************************************
    * 防具グラフを作成
    **************************************/
   public Bitmap MakeArmorGraph(int nWidth)
   {
	   String strText = "";
	   	Bitmap csBmp = Bitmap.createBitmap(280, 180, Config.ARGB_8888);
	   	Canvas csCanvas = new Canvas(csBmp);
	   	
		TextPaint paintText = new TextPaint();
	    paintText.setTextSize(18);

	    // 背景
		Rect GraphRect = new Rect();
		GraphRect.set(25, 35, 185, 135);
		  
		// スロット数を表示
	    DrawArmorSlotText(csCanvas, 0, 28);
		
		// 防御力を表示
	    DrawArmorPowerText(csCanvas, 40, 20);
	    
	    // レア度を表示
	    DrawArmorRareText(csCanvas, 0, 14);
		
	    // 値段を表示
		DrawPriceText(csCanvas, GraphRect.right+5, 20);
		  
		  Paint paint = new Paint();
	      paint.setAntiAlias(true);
	      paint.setStyle(Paint.Style.FILL);
	      
		  // 外側の線を描画
		  paint.setStrokeWidth(4);
		  paint.setColor(Color.argb(255, 0, 0, 255));
		  csCanvas.drawLine(GraphRect.left, GraphRect.top, 	  GraphRect.left,  GraphRect.bottom, paint);	  
		  csCanvas.drawLine(GraphRect.left, GraphRect.bottom, GraphRect.right, GraphRect.bottom, paint);	  
		  
		  // 単位と線を描画
		  int nPoint = 0;
		  float fGraphY = 0f;
		  paint.setColor(Color.argb(255, 0,0,0));
		  csCanvas.drawText("100", GraphRect.left-25, GraphRect.top+5, paint);
		  csCanvas.drawText("50", GraphRect.left-20, GraphRect.centerY()+5, paint);
		  csCanvas.drawText("0", GraphRect.left-15, GraphRect.bottom+10, paint);
		  csCanvas.drawText("LV", GraphRect.right+5, GraphRect.bottom+10, paint);
		  for(int nLine = 0; nLine <= 10; nLine++){
			  fGraphY = GraphRect.top + GraphRect.height() / 10 * nLine;
			  if(nLine == 5){
				  paint.setStrokeWidth(3);			  
			  }else{
				  paint.setStrokeWidth(1);			  
			  }
			  // 単位を出力
			  //paint.setColor(Color.argb(255, 255, 255, 255));
			  //csCanvas.drawText(String.format("%3d",nPoint), 0, fGraphY+5, paint);
			  // 線を出力
			  paint.setColor(Color.argb(255, 128, 128, 128));
			  csCanvas.drawLine(GraphRect.left + 1, fGraphY, GraphRect.right, fGraphY, paint);
		  }
		  
		  // 成績を表示
		  int nGrowMFlag=0;
		  int nDispNum = (int)Math.floor(GraphRect.width() / 15);
		  {
			  // 点と点数を表示
			  int nLength = m_dbArmor.GetLevelNum();
			  for(int nIdx=0; nIdx<nDispNum && nIdx<nLength; nIdx++){
				  int nPower = m_dbArmor.GetLevelPower(nIdx);
				  if(-1==nPower){
					  paint.setTextSize(42);
					  paint.setColor(Color.argb(255, 255, 0, 0));
					  csCanvas.drawText("調査中", GraphRect.left+20, GraphRect.centerY()+10, paint);					  
					  break;
				  }
				  int nNum = nIdx + 2;
				  int nPosX = GraphRect.left + 15*(nIdx+1);
				  int nPosY = GraphRect.bottom - GraphRect.height() * nPower / 100;
				  // 点を描画
				  paint.setColor(Color.argb(255, 0, 255, 0));
				  csCanvas.drawCircle(nPosX, nPosY, 3, paint);
				  // 点数を描画
				  paint.setColor(Color.argb(255, 0, 0, 255));
				  int nAddY = 0;
				  if(nPower > 80) nAddY = 15; else nAddY = -5;
				  csCanvas.drawText(String.valueOf(nPower), nPosX-5, nPosY+nAddY, paint);
				  // テスト回数を表示
				  paint.setColor(Color.argb(255, 255, 0, 0));
				  csCanvas.drawText(String.valueOf(nNum), nPosX-5, GraphRect.bottom+15, paint);
			  }
			  // 線を表示
			  if(2 <= nLength){
				  int nConstantNum = 0;
				  int nAddX=0;
				  for(int nIdx=0; nIdx<nDispNum-1 && nIdx<nLength-1; nIdx++){
					  int nNextIdx = nIdx + 1;
					  if(nNextIdx >= nLength){
						  break;
					  }
					  int nPosX = GraphRect.left + 15*(nIdx+1);
					  int nPosY = GraphRect.bottom - GraphRect.height() * m_dbArmor.GetLevelPower(nIdx) / 100;
					  int nNextPosX = nPosX + 20;
					  int nNextPosY = GraphRect.bottom - GraphRect.height() * m_dbArmor.GetLevelPower(nNextIdx) / 100;
					  // 点と線を描画
					  paint.setColor(Color.argb(255, 0, 255, 0));
					  csCanvas.drawLine(nPosX, nPosY, nNextPosX, nNextPosY, paint);
					  // 武具玉グラフを描画
					  int nGrowMNum = m_dbArmor.GetLevelGrowMaterial(nNextIdx);
					  SetColorGrowMaterialBar(nGrowMNum, paint);
					  csCanvas.drawRect(new Rect(nPosX-3, GraphRect.bottom+20, nNextPosX-3, GraphRect.bottom+30), paint);
					  // 強化料金を表示
					  if(nGrowMNum!=nGrowMFlag){
						  nGrowMFlag=nGrowMNum;
						  paint.setTextSize(10);
						  paint.setColor(Color.argb(255, 0, 0, 0));
						  if(1==nConstantNum)	nAddX+=10;
						  else					nAddX=0;
						  csCanvas.drawText("" + m_dbArmor.GetLevelGrowMaterialPrice(nNextIdx) + "z", nPosX-3+nAddX, GraphRect.bottom+42, paint);
						  nConstantNum=0;
					  }
					  nConstantNum++;
				  }
			  }
		  }
		  
		  // 武具玉説明描画
		  DrawGrowMaterialText(csCanvas, GraphRect.right, GraphRect.top);
		  
		return csBmp;	   
   }
      
   /*************************************
    * 属性のグラフを作成
    **************************************/
   public Bitmap MakeResistGraph()
   {
	   	Bitmap csBmp = Bitmap.createBitmap(120, 170, Config.ARGB_8888);
	   	Canvas csCanvas = new Canvas(csBmp);
	   	
		TextPaint paintText = new TextPaint();
		paintText.setTextSize(18);
	    paintText.setColor(Color.argb(255, 0, 0, 0));
	    csCanvas.drawText("属性", 0, 18, paintText);
	    
	    // 属性グラフを描画
	    DrawResistGraph(csCanvas, csBmp.getWidth(), 0, 35);
	    	   	
		return csBmp;
   }
   
   /*************************************
    * 属性のグラフの描画
    **************************************/
   public void DrawResistGraph(Canvas csCanvas, int nWidth, int nStartX, int nStartY)
   {
	   String strText = "";
		TextPaint paintText = new TextPaint();
		paintText.setTextSize(18);

		// 背景文字列
	    paintText.setColor(Color.argb(255, 200, 0, 0));
	    csCanvas.drawText("火", nStartX, nStartY+20, paintText);
	    paintText.setColor(Color.argb(255, 0, 50, 200));
	    csCanvas.drawText("水", nStartX, nStartY+45, paintText);
	    paintText.setColor(Color.argb(255, 200, 200, 0));
	    csCanvas.drawText("雷", nStartX, nStartY+70, paintText);
	    paintText.setColor(Color.argb(255, 200, 200, 200));
	    csCanvas.drawText("氷", nStartX, nStartY+95, paintText);
	    paintText.setColor(Color.argb(255, 50, 200, 50));
	    csCanvas.drawText("龍", nStartX, nStartY+120, paintText);
	    
	    // グラフ位置を計算
	    int nGraphS = nStartX+40;
	    int nGraphE = nWidth-10;
	    int nGraphWidth = nGraphE - nGraphS;
	    int nGraphOne = nGraphWidth/10;
	    int nMiddleX = (nGraphE + nGraphS) / 2;
	    
	    // グラフ背景－グラフ線を描画
	    Paint paint = new Paint();
	    paint.setColor(Color.argb(255, 180, 180, 180));
	    csCanvas.drawLine(nStartX+35, nStartY+1, nWidth-5, nStartY+1, paint);	// 単位線
	    csCanvas.drawLine(nStartX+35, nStartY+126, nWidth-5, nStartY+126, paint);	// 単位線
	    paint.setColor(Color.argb(255, 0, 0, 0));
	    for(int nIndex=0; nIndex<5; nIndex++){
	    	// 中央線
	    	csCanvas.drawRect(new Rect( nMiddleX-1,  nStartY+5+25*nIndex,
	    								nMiddleX+1,  nStartY+25*(nIndex+1) ), paint);
	    }
	    
	    // 単位を描画
		paintText.setTextSize(12);
		paintText.setColor(Color.argb(255, 0, 0, 0));
	    csCanvas.drawText("-5", nGraphS-7,	nStartY, paintText);
	    csCanvas.drawText("0",  nMiddleX-2,	nStartY, paintText);
	    csCanvas.drawText("+5", nGraphE-3, 	nStartY, paintText);

	    // 数値を描画
	    int nNum[] = new int[5];
	    for(int nIndex=0; nIndex<5; nIndex++){
	    	nNum[nIndex] = m_dbArmor.GetArmorResistNum(m_nNowItemPos, nIndex);
		    strText = GetPlusMinusString(nNum[nIndex]);
		    csCanvas.drawText(strText, nStartX+20,  nStartY+18+25*nIndex, paintText);
	    }
	    
	    // グラフ棒描画
	    int nColor[] = new int[5];
	    nColor[0] = Color.argb(255, 200, 0, 0);		// 火
	    nColor[1] = Color.argb(255, 0, 50, 200);	// 水
	    nColor[2] = Color.argb(255, 200, 200, 0);	// 雷
	    nColor[3] = Color.argb(255, 200, 200, 200);	// 氷
	    nColor[4] = Color.argb(255, 50, 200, 50);	// 龍
	    for(int nIndex=0; nIndex<5; nIndex++){
		    paint.setColor(nColor[nIndex]);
		    csCanvas.drawRect(new Rect( nMiddleX, nStartY+5+25*nIndex,
		    							nMiddleX + nGraphOne*nNum[nIndex], nStartY+25*(nIndex+1) ), paint);
	    }
   }
   
   /*************************************
    * -5～+5の文字列を取得する
    **************************************/
   public String GetPlusMinusString(int nNum)
   {
	   String strNum = "";	   
	   if(0 < nNum)	strNum = String.format("+%d", nNum);
	   else			strNum = String.format("%2d", nNum);
	   return strNum;
   }

   /*************************************
    * スキルリストを取得
    **************************************/
   private String GetSkillListDescription()
   {    	
	   String strHtml="";
	   strHtml += "<font color=#00ffff><B>スキル</B></font><BR>";
	   for(int nIndex=0; nIndex<5; nIndex++){
		   strHtml += "<font color=white><B>" + m_dbArmor.GetArmorSkillStr(m_nNowItemPos, nIndex);
		   strHtml += "</B></font><br>";
	   }
	   return strHtml;
   }
   
   /*************************************
    * 素材リストを取得
    **************************************/
   private String GetMaterialListDescription()
   {    	
	   String strHtml="";
	   strHtml += "<font color=#ffff00><B>端材</B></font><BR>";
	   strHtml += "<font color=white><B>" + m_dbArmor.GetArmorMaterialStr(m_nNowItemPos, 0) + "</B></font><BR>";
	   strHtml += "<BR>";
	   strHtml += "<font color=#00ff00><B>生産素材</B></font><BR>";
	   for(int nIndex=1; nIndex<5; nIndex++){
		   strHtml += "<font color=white><B>" + m_dbArmor.GetArmorMaterialStr(m_nNowItemPos, nIndex);
		   strHtml += "</B></font><br>";
	   }
	   return strHtml;
   }
   
   /*************************************
    * 防具部位を表示
    **************************************/
   private void SetPartText(){
	   TextView strPartTextView = (TextView) findViewById(R.id.ArmorPartTextView);
       strPartTextView.setText(m_dbArmor.GetArmorPart(m_nNowItemPos));
   }
   
   /*************************************
    * 防具名を表示
    **************************************/
   private void SetArmorNameText(){
	    TextView TitleTextView = (TextView) findViewById(R.id.ArmorTitleTextView);
	    TitleTextView.setText(m_dbArmor.GetArmorNameStr(m_nNowItemPos));
	    SetRareColorTextView(TitleTextView, m_dbArmor.GetArmorRare(m_nNowItemPos));
   }
   
   /*************************************
    * レア度によるテキストカラーをセット
    **************************************/
   private void SetRareColorTextView(TextView csView, int nRare)
   {
	    int nColor=Color.BLACK;
	    switch(nRare){
	    case 1:
	    	break;
	    case 2:
	    	nColor = Color.rgb(0xcc, 0x44, 0xff);
	    	break;
	    case 3:
	    	nColor = Color.rgb(0x99, 0x99, 0x22);
	    	break;
	    case 4:
	    	nColor = Color.rgb(0x77, 0x22, 0x77);
	    	break;
	    case 5:
	    	nColor = Color.rgb(0x33, 0x99, 0x33);
	    	break;
	    case 6:
	    	nColor = Color.rgb(0x77, 0x88, 0xcc);
	    	break;
	    case 7:
	    	nColor = Color.rgb(0xff, 0x00, 0x00);
	    	break;
	    }
	    csView.setTextColor(nColor);
   }
   
   /*************************************
    * 武具玉のグラフ色をセット
    **************************************/
   public void SetColorGrowMaterialBar(int nGrowMaterialLevel, Paint paint)
   {
	   switch(nGrowMaterialLevel){
	   case 1:
		   paint.setColor(Color.argb(255, 0, 0, 255));
		   break;
	   case 2:
		   paint.setColor(Color.argb(255, 0, 180, 0));
		   break;
	   case 3:
		   paint.setColor(Color.argb(255, 200, 128, 200));
		   break;
	   case 4:
		   paint.setColor(Color.argb(255, 255, 0, 0));
		   break;
	   }
   }
   
   /*************************************
    * 防御力を描画
    **************************************/
   private void DrawArmorPowerText(Canvas csCanvas, int nX, int nY)
   {
	   TextPaint paintText = new TextPaint();
	   paintText.setTextSize(18);
	   paintText.setColor(Color.argb(255, 80, 80, 80));
	   String strText = String.format("防御力：%-3d", m_dbArmor.GetArmorPowerNum(m_nNowItemPos));
	   csCanvas.drawText(strText, nX, nY, paintText);
   }
   
   /*************************************
    * レア度を描画
    **************************************/
   private void DrawArmorRareText(Canvas csCanvas, int nX, int nY){
	   TextPaint paintText = new TextPaint();
	   paintText.setTextSize(12);
	   paintText.setColor(Color.argb(255, 80, 80, 80));
	   String strText = String.format("レア%d", m_dbArmor.GetArmorRare(m_nNowItemPos));
	   csCanvas.drawText(strText, nX, nY, paintText);
	   
   }
   
   /*************************************
    * 防御力を描画
    **************************************/
   private void DrawArmorSlotText(Canvas csCanvas, int nX, int nY)
   {
	   TextPaint paintText = new TextPaint();
	   paintText.setTextSize(18);
	   paintText.setColor(Color.argb(255, 0, 0, 0));
	   int nSlotNum = m_dbArmor.GetArmorSlot(m_nNowItemPos);
	   String strText = "";
	   for(int n=1; n<=3; n++){
		   if(n <= nSlotNum)	strText += "○";
		   else					strText += "-";
	   }
	   csCanvas.drawText(strText, nX, nY, paintText);
   }
   
   /*************************************
    * 値段を描画
    **************************************/
   private void DrawPriceText(Canvas csCanvas, int nX, int nY)
   {
	   TextPaint paintText = new TextPaint();
	   paintText.setTextSize(18);
	   csCanvas.drawText("【値段】", nX, nY, paintText);
	   paintText.setTextSize(12);
	   int nPrice = m_dbArmor.GetArmorPriceNum(m_nNowItemPos);
	   String strText = String.format("%6dz", nPrice);
	   if( -1 == nPrice ){
		   strText = "  －  ";
	   }
	   csCanvas.drawText(strText, nX+25, nY+15, paintText);
   }
   
   /*************************************
    * 強化素材（武具玉）の説明を描画
    **************************************/
   private void DrawGrowMaterialText(Canvas csCanvas, int nX, int nY)
   {
	  TextPaint paint = new TextPaint();
	  paint.setTextSize(12);
	  SetColorGrowMaterialBar(1, paint);
	  csCanvas.drawRect(new Rect(nX+10, nY+10, nX+20, nY+20), paint);
	  csCanvas.drawText("武具玉", nX+25, nY+20, paint);
	  SetColorGrowMaterialBar(2, paint);
	  csCanvas.drawRect(new Rect(nX+10, nY+30, nX+20, nY+40), paint);
	  csCanvas.drawText("上武具玉", nX+25, nY+40, paint);
	  SetColorGrowMaterialBar(3, paint);
	  csCanvas.drawRect(new Rect(nX+10, nY+50, nX+20, nY+60), paint);
	  csCanvas.drawText("堅武具玉", nX+25, nY+60, paint);
	  SetColorGrowMaterialBar(4, paint);
	  csCanvas.drawRect(new Rect(nX+10, nY+70, nX+20, nY+80), paint);
	  csCanvas.drawText("重武具玉", nX+25, nY+80, paint);
   }
   
   /*************************************
    * 防具部位で絞り込んでいるかどうかを判定
    **************************************/
   private boolean IsOnlyDispPart(){
	   return (0 < m_nSelectedPart);
   }
   
   /*************************************
    * 防具部位で絞り込んでいるかどうかを判定
    **************************************/
   private boolean IsOnlyDispSkill(){
	   return (0 < m_nSelectedSkill);
   }
}
